/**
 * @file port_log.c
 * @brief
 * @author lijianpeng (bosco@pthyidh.com)
 * @date 2024-12-10
 *
 * @copyright Copyright (c) 2024 by 深圳市鹏天微智能科技有限公司, All Rights Reserved.
 */


/* ==================== [Includes] ========================================== */

#include "xf_hal_port.h"

#if defined(CONFIG_PORT_LOG_ENABLE)

#include "pt3220.h"
#include "drvs.h"

#include "xf_init.h"
#include "xf_log.h"

#include "port_log.h"

/* ==================== [Defines] =========================================== */

#if defined(CONFIG_PORT_LOG_UART_PORT1)
    #define LOG_UART_PORT       (UART2_PORT)
#else
    #define LOG_UART_PORT       (UART1_PORT)
#endif

#if defined(CONFIG_PORT_LOG_UART_TX_PIN)
    #define LOG_UART_TX_PIN     (CONFIG_PORT_LOG_UART_TX_PIN)
#else
    #define LOG_UART_TX_PIN     (6)
#endif

#if defined(CONFIG_PORT_LOG_UART_RX_PIN)
    #define LOG_UART_RX_PIN     (CONFIG_PORT_LOG_UART_RX_PIN)
#else
    #define LOG_UART_RX_PIN     (7)
#endif

#if defined(CONFIG_PORT_LOG_UART_BAUD_921600)
    #define LOG_UART_BAUD       (BRR_921600)
#else
    #define LOG_UART_BAUD       (BRR_115200)
#endif

/* ==================== [Typedefs] ========================================== */

/* ==================== [Static Prototypes] ================================= */

/* ==================== [Static Variables] ================================== */

/* ==================== [Macros] ============================================ */

/* ==================== [Global Functions] ================================== */

/* ==================== [Static Functions] ================================== */

void uart_log_init(void)
{
    // uart_init
    uart_init(LOG_UART_PORT, LOG_UART_TX_PIN, LOG_UART_RX_PIN);

    // uart_conf
    uart_conf(LOG_UART_PORT, LOG_UART_BAUD, LCR_BITS_DFLT);
}

void uart_log_send(const char *str, size_t len, void *arg)
{
    uart_send(LOG_UART_PORT, len, (const uint8_t *)str);
}

static int port_log_init(void)
{
    int log_uart_id = xf_log_register_obj(uart_log_send, NULL);
    xf_log_set_info_level(log_uart_id, XF_LOG_LVL_ERROR);

    return 0;
}

XF_INIT_EXPORT_SETUP(port_log_init);

#endif
